package face.yuque;

import java.util.Arrays;

/**
 * @author spf
 * @date 2024/12/11
 * @time 11:14
 */

//寻找数组中心下标
public class ArrayCenter {
    public static void main(String[] args) {
//        int[] nums = {1,2,3,2,1};
//        int[] nums = {0,2,3,-2,-3};
        int[] nums = {1, 7, 3, 6, 5, 6};
        System.out.println(arrayCenter(nums));
    }

    private static int arrayCenter(int[] nums) {
        int sum = Arrays.stream(nums).sum();
        int total = 0;
        for (int i = 0; i < nums.length; i++) {
            total += nums[i];
            if (total == sum) {
                return i;
            }
            sum -= nums[i];
        }
        return -1;
    }
}

